/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SeleccionRol.java
 *
 * Created on 14-sep-2009, 22:26:32
 */

package com.jomijushi.fid.interfaz;

import com.jomijushi.fid.dominio.FidRolTrabajador;
import com.jomijushi.fid.servicios.RolTrabajadorImplementsService;
import com.jomijushi.fid.servicios.RolTrabajadorInterfazService;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author miguel
 */
public class SeleccionRol extends javax.swing.JDialog {

    /** Creates new form SeleccionRol */
    FidRolTrabajador frt;
    public boolean asignado= false;
    
    public SeleccionRol(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(this);
        CargarTabla();
    }

    public FidRolTrabajador getRol()
    {
        return this.frt;
    }

    public void CargarTabla()
    {
        // Aqui definimos ¿Qué columnas? seran editables
        // true -> Editable; false -> No Editable

        RolTrabajadorInterfazService frlservice = new RolTrabajadorImplementsService();
        frlservice.setEntityManagerFactory(PanelPrincipal.emf);
        List<FidRolTrabajador> lst = frlservice.Listar(false,"",true,0,0);
        //ya vienen ordenados alfabeticamente
        for(int i=0; i<lst.size(); i++)
            System.out.println("rol trabajador = "+lst.get(i).getNombre());

        FidRolTrabajador frt;
        //List<FidRolTrabajador> lst = LRT;
        int cant= lst.size();

        ModeloTabla.canEdit = new boolean [] {false, false, false};
        ModeloTabla modeloTabla= new ModeloTabla();

        if(cant>0){
            modeloTabla.addColumn("ID");
            modeloTabla.addColumn("Rol Trabajador");
            modeloTabla.addColumn("Descripción");

            for ( int i=0; i<cant; i++){
                frt = new FidRolTrabajador();
                Object[] fila = new Object[3];
                frt = lst.get(i);
                fila[0] = frt.getId();
                fila[1] = frt.getNombre();
                fila[2] = frt.getDescripcion();

                modeloTabla.addRow(fila);
            }
        }
        else
        {
            Object[] obj={null,null,null,null};
            modeloTabla.addColumn("ID");
            modeloTabla.addColumn("Rol Trabajador");
            modeloTabla.addColumn("Descripción", obj);
        }
        Tabla.setModel(modeloTabla);
        // Seccion para Configurar Visualizacion de Tabla
        TableColumnModel tcm = Tabla.getColumnModel();
        // Se pide la columna por su número, empezando en cero.
        //TableColumn columnaABorrar = tcm.getColumn(0);
        Tabla.removeColumn (tcm.getColumn(0));  // oculta columna de tabla
        tcm.getColumn(1).setPreferredWidth(100);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tabla = new javax.swing.JTable();
        BotonAsignarRol = new javax.swing.JButton();
        BotonCancelarRol = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Asignar Rol");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Seleccione Rol"));

        Tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Rol", "Descripción"
            }
        ));
        jScrollPane1.setViewportView(Tabla);

        BotonAsignarRol.setText("Agregar");
        BotonAsignarRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonAsignarRolActionPerformed(evt);
            }
        });

        BotonCancelarRol.setText("Cancelar");
        BotonCancelarRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonCancelarRolActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(167, 167, 167)
                .addComponent(BotonAsignarRol, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BotonCancelarRol, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotonCancelarRol)
                    .addComponent(BotonAsignarRol))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BotonAsignarRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonAsignarRolActionPerformed
        // TODO add your handling code here:
        if(Tabla.getSelectedRowCount()==0){
            JOptionPane.showMessageDialog(this, "Seleccione un Rol.", "Error", JOptionPane.WARNING_MESSAGE);
        }
        else if(Tabla.getSelectedRowCount()>1)
        {
            JOptionPane.showMessageDialog(this, "Seleccione sólo un rol para agregar a la vez", "Error", JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            frt= new FidRolTrabajador();
            String codigo = Tabla.getModel().getValueAt(Tabla.getSelectedRow(),0).toString();
            System.out.println("Contenido de Codigo: "+ codigo);
            int idSeleccionado = Integer.parseInt(codigo);
            frt.setId(idSeleccionado);
            frt.setNombre(Tabla.getModel().getValueAt(Tabla.getSelectedRow(),1).toString());
            frt.setDescripcion(Tabla.getModel().getValueAt(Tabla.getSelectedRow(),2).toString());
            asignado=true;
            this.setVisible(false);
        }
    }//GEN-LAST:event_BotonAsignarRolActionPerformed

    private void BotonCancelarRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonCancelarRolActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BotonCancelarRolActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SeleccionRol dialog = new SeleccionRol(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonAsignarRol;
    private javax.swing.JButton BotonCancelarRol;
    private javax.swing.JTable Tabla;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
